/*
* All code licensed under the GPLv2 and CDDL.
* Please visit http://www.gnu.org/licenses/gpl-2.0.html
* and http://www.sun.com/cddl/cddl.html for the full versions of
* these licenses.
 */

package twitterapp;

//Imports the networking classes...
import java.net.*;
import java.io.*;
//import javax.swing.JOptionPane;

/**
 *
 * @author Xolani
 */
public class RestAPI {

    public String ConnectServer (String username, String password)
            throws Exception
    {
        //Encode username & password to Base64 for basic authentication.
        String authcode = EncodeAuth (username, password);

        try
        {
        HttpURLConnection verify_auth = OpenHttpConnection
                ("http://twitter.com/account/verify_credentials.xml",
                authcode);

		BufferedReader reader = new BufferedReader(new InputStreamReader
                (verify_auth.getInputStream()));
		StringWriter writer = new StringWriter();
		String line = null;
        String data = null;
		while ((line = reader.readLine()) != null) {
			writer.append(line);
            if (data != null) data += "\n";
            data += line;
		}
		reader.close();
		verify_auth.disconnect();
        data = data.replace("null<?", "<?"); //Remove "null" from top of string.
        verify_auth = null; //Remove reference to the object so GC can remove.

        return data;
        }
        catch (Exception e)
        {
            return e.toString();
        }
    }

    public String PublicTimeline ()
    {
        String Timeline = "";
        return Timeline;
    }

    public String FriendsTimeline ()
    {
        String Timeline = "";
        return Timeline;
    }

    public String UserTimeline ()
    {
        String Timeline = "";
        return Timeline;
    }

    private String EncodeAuth (String username, String password)
    {
        //Put the username and password into a standard string formatted for
        //use in basic auth.
        username = username.toLowerCase(); //Make sure the username is all lower
                                           //case.
        String pre_encode = username + ":" + password;
        //Blank the password out for security.
        password = "";
        //Then convert it to a byte array.
        byte ByteArray[] = pre_encode.getBytes();
        //Variable for returning to caller. It contains the status of
        //whether we were able to connect or not.
        //boolean ConnectStatus = false;

        String authcode = Base64.encodeBytes(ByteArray);
        pre_encode = ""; //Remove the string from pre_encode for security.
        authcode = "Basic " + authcode;

        return authcode;
    }

    private HttpURLConnection OpenHttpConnection (String address,
            String authcode) throws Exception
    {
        HttpURLConnection connection = (HttpURLConnection) new
                        URL(address)
                        .openConnection();

        connection.setRequestProperty("User-agent", "XTA");
        connection.setRequestProperty("Authorization", authcode);
        connection.setDoInput(true); connection.setDoOutput(true);

        return connection;
    }
}
